// TopChatFrameWindow.cs
//
//  Copyright (C) 2008 Jacek Trubłajewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using System;
using System.Drawing;
using System.IO;
using Mono.Unix;
using NLog;

namespace wxGadu
{
	
	/// <summary>
	/// Top part of <c>ChatFrame</c>. 
	/// Keeps messages window.
	/// </summary>
	public class TopChatFramePanel : wx.Panel
	{
		#region IDs
		private enum Id
		{
			messageWindow
		}
		#endregion	

		#region windows
		private CopyHtmlWindow messageWindow;
		private wx.BoxSizer topSizer;
//		private wx.Button scrollButton;
		#endregion

		#region data members
		private string htmlCode;
		private MainFrame mainFrame;
		#endregion
		private delegate void ScrollAsyncDelegate();
		private ScrollAsyncDelegate scrollObject;
		
#if LOG
		private Logger logger = LogManager.GetCurrentClassLogger();
#endif
		
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="parent">
		/// A <see cref="wx.Window"/>
		/// </param>
		public TopChatFramePanel(wx.Window parent): 
			base(parent)
		{
			wx.SplitterWindow splitter = (wx.SplitterWindow) parent;
			ChatFrame chatFrame =  (ChatFrame) splitter.Parent;
			mainFrame = (MainFrame) chatFrame.MainFrame;
			
			messageWindow = new CopyHtmlWindow(this, (int) Id.messageWindow, wx.Window.wxDefaultPosition,
			                                  new Size(150, 200), wx.HtmlWindow.wxHW_SCROLLBAR_AUTO|wx.Window.wxBORDER_MASK,
			                                  mainFrame);
			messageWindow.Borders = 0;
			
			wx.Font msgFont;
			string fontFace;
			int fontSize;
			if (mainFrame.AppCfg.UserConf.TopChatWindowFont == null)
				msgFont = wx.SystemSettings.GetFont(wx.SystemFont.wxSYS_DEFAULT_GUI_FONT);
			else
				msgFont = (wx.Font) mainFrame.AppCfg.UserConf.TopChatWindowFont;
			
			
			fontSize = msgFont.PointSize;
			fontFace = msgFont.FaceName;
			int[] fontSizes = new int[7];
			for(int i=0; i < fontSizes.Length; i++)
				fontSizes[i] = fontSize;
			messageWindow.SetFonts(fontFace, fontFace, fontSizes);
			
			DoLayout();
			
			scrollObject = new ScrollAsyncDelegate(ScrollDownAsync);

			EVT_UPDATE_UI((int) FakeId.scrollMessageWindow, new wx.EventListener(ScrollDown));
		}

		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				topSizer.Add(messageWindow, 1, wx.Stretch.wxEXPAND);
			}
			
			topSizer.Fit(this);
			topSizer.SetSizeHints(this);
			this.Sizer = topSizer;
			
		}
	
		/// <summary>
		/// Scrolls down window content to make sure 
		/// that the bottom of window is visible
		/// </summary>
		public void ScrollDown(object sender, wx.Event evt)
		{
			int x =0, y = 0, i =0, j= 0;
			messageWindow.GetVirtualSize(ref x, ref y);
			messageWindow.GetScrollPixelsPerUnit(ref i, ref j);

			messageWindow.Scroll(-1, y / j);
//			System.Console.WriteLine("GetVirtualSize {0} : {1}", x, y);
		}
		
		private void ScrollDownAsync()
		{
			// wait a few ms, then scroll down window
			System.Threading.Thread.Sleep(10);	
			this.AddPendingEvent(new wx.UpdateUIEvent((int) FakeId.scrollMessageWindow));
		}
		
		
		/// <summary>
		/// Sets new message window content
		/// </summary>
		/// <param name="htmlCode">
		/// A <see cref="System.String"/>
		/// chat
		/// </param>
		public void SetNewPage(string htmlCode)
		{
			
			messageWindow.SetPage(htmlCode);
			// call async method for scrolling
			scrollObject.BeginInvoke(null, null);
		}
	}
}
